﻿using System;
using RG.Common.Configuration;
using RG.Common.WPF.Commands;
using Microsoft.Practices.Unity;

namespace RG.Common.WPF.Filters.ToolBarFilters
{
    [SupportDataBinding]
    public class FilterCheckBoxFrameVM : FilterBaseParamVM<bool?>
    {
        public string YesText { get; set; }
        public string NoText { get; set; }

        public FilterCheckBoxFrameVM(IUnityContainer cfg):base(cfg)
        {
            NotUsed = true;
        }

        public string FormattedValue
        {
            get
            {
                if (NotUsed) return EmptyText;
                if (Val) return YesText;
                if (NoVal) return NoText;
                return EmptyText;
            }
        }

        private bool val;

        [PropertySetting(SaveNullValue = true)]
        public bool Val
        {
            get { return val; }
            set
            {
                val = value;
                SetValue();
            }
        }

        private void SetValue()
        {
            if (NotUsed || (!NotUsed && !Val && !NoVal))
                Value = null;
            else
            {
                Value = Val;
            }

        }

        private bool noVal;

        [PropertySetting(SaveNullValue = true)]
        public bool NoVal
        {
            get { return noVal; }
            set
            {
                noVal = value;
                SetValue();
            }
        }

        private bool notUsed;

        [PropertySetting(SaveNullValue = true)]
        public bool NotUsed
        {
            get { return notUsed; }
            set
            {
                notUsed = value;
                SetValue();
            }
        }

        public override void ClearValueQuiet()
        {
            NotUsed = true;
        }

        public override bool ValueEmpty()
        {
            return  NotUsed;
        }

        protected override bool? ReturnDefaultEmptyValue()
        {
            return null;
        }
    }
}